﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HCQ2_IDAL;
using HCQ2_Model;
using HCQ2_Model.APPModel.ParamModel;
using System.Data;
using HCQ2_Model.VR.Params;
using HCQ2_Common;
using HCQ2_Common.SQL;
using System.Globalization;

namespace HCQ2_DAL_MSSQL
{
    public partial class T_VROnLineUserDAL: IT_VROnLineUserDAL
    {
        //******************************************pc*********************************************
        /// <summary>
        ///  获取在线用户列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<T_VROnLineUser> GetOnLineList(ExperRecordModel param)
        {
            sb?.Clear();
            sb.AppendFormat(@"SELECT TOP {0} lines.* FROM 
            (SELECT ROW_NUMBER() OVER(ORDER BY line.in_date DESC) rowNumber,line.* FROM 
            (SELECT user_id,file_id,folder_name,MAX(in_date) AS in_date FROM T_VROnLineUser GROUP BY user_id,file_id,folder_name)sigle INNER JOIN  (SELECT on_id,user_id,user_name,file_id,file_name,folder_name,in_date,line_status FROM T_VROnLineUser WHERE 1=1 ", param.rows);
            //用户id
            if (param.userId > 0)
                sb.AppendFormat(" AND user_id={0} ", param.userId);
            //用户名
            if (!string.IsNullOrEmpty(param.userName))
                sb.AppendFormat(" AND user_name like '%{0}%' ",param.userName);
            //体验作品
            if (!string.IsNullOrEmpty(param.fileName))
                sb.AppendFormat(" AND file_name like '%{0}%' ", param.fileName);
            //体验项目
            if (!string.IsNullOrEmpty(param.folderName))
                sb.AppendFormat(" AND folder_name like '%{0}%' ", param.folderName);
            //状态
            if (!string.IsNullOrEmpty(param.lineStatus))
                sb.AppendFormat(" AND line_status='{0}' ", param.lineStatus);
            sb.AppendFormat(@")line ON line.user_id = sigle.user_id AND line.file_id = sigle.file_id AND line.folder_name = sigle.folder_name AND line.in_date = sigle.in_date) lines WHERE lines.rowNumber>{0};", (param.page - 1) * param.rows);
            DataTable dt = SqlHelper.ExecuteDataTable(sb.ToString());
            return HCQ2_Common.Data.DataTableHelper.DataTableToIList<T_VROnLineUser>(dt);
        }
        /// <summary>
        ///  获取在线用户数量
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public int GetOnLineNum(ExperRecordModel param)
        {
            sb?.Clear();
            sb.AppendFormat(@"SELECT COUNT(0) AS num FROM 
            (SELECT user_id,file_id,folder_name,MAX(in_date) AS in_date FROM T_VROnLineUser GROUP BY user_id,file_id,folder_name)sigle INNER JOIN
            (SELECT on_id,user_id,user_name,file_id,file_name,folder_name,in_date,line_status FROM T_VROnLineUser WHERE 1=1 ");
            //用户id
            if (param.userId > 0)
                sb.AppendFormat(" AND user_id={0} ", param.userId);
            //用户名
            if (!string.IsNullOrEmpty(param.userName))
                sb.AppendFormat(" AND user_name like '%{0}%' ", param.userName);
            //体验作品
            if (!string.IsNullOrEmpty(param.fileName))
                sb.AppendFormat(" AND file_name like '%{0}%' ", param.fileName);
            //体验项目
            if (!string.IsNullOrEmpty(param.folderName))
                sb.AppendFormat(" AND folder_name like '%{0}%' ", param.folderName);
            //状态
            if (!string.IsNullOrEmpty(param.lineStatus))
                sb.AppendFormat(" AND line_status='{0}' ", param.lineStatus);
            sb.AppendFormat(@")line ON line.user_id = sigle.user_id AND line.file_id = sigle.file_id AND line.folder_name = sigle.folder_name 
            AND line.in_date = sigle.in_date;");
            return Helper.ToInt(SqlHelper.ExecuteScalar(sb.ToString()));
        }
        //******************************************接口*********************************************
        /// <summary>
        ///  用户在线用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public List<T_VROnLineUser> GetOnLineUser(QueryRecordModel model)
        {
            sb?.Clear();
            sb.AppendFormat($"SELECT TOP {model.size} * FROM T_VROnLineUser WHERE 1=1 ");
            if (model.file_id > 0)
                sb.AppendFormat(" AND file_id={0} ",model.file_id);
            if (!string.IsNullOrEmpty(model.folder_name))
                sb.AppendFormat(" AND folder_name='{0}' ", model.folder_name);
            if (!string.IsNullOrEmpty(model.line_status))
            {
                if (model.line_status.Equals("online"))
                    model.line_status = "在线";
                else
                    model.line_status = "离线";
            }
            //时间
            string inDate = string.Empty, outDate = string.Empty;
            if (!string.IsNullOrEmpty(model.in_date))
                inDate = DateTime.ParseExact(model.in_date, "yyyyMMddHHmmss", CultureInfo.CurrentCulture, DateTimeStyles.None).ToString();
            if (!string.IsNullOrEmpty(model.out_date))
                outDate = DateTime.ParseExact(model.out_date, "yyyyMMddHHmmss", CultureInfo.CurrentCulture, DateTimeStyles.None).ToString();
            if (!string.IsNullOrEmpty(model.in_date) && !string.IsNullOrEmpty(model.out_date))
                sb.AppendFormat(@" AND in_date>='{0}' AND in_date<='{1}' ", inDate, outDate);
            else if (!string.IsNullOrEmpty(model.in_date) && string.IsNullOrEmpty(model.out_date))
                sb.AppendFormat(@" AND in_date>='{0}' ", inDate);
            else if (string.IsNullOrEmpty(model.in_date) && !string.IsNullOrEmpty(model.out_date))
                sb.AppendFormat(@" AND in_date<='{0}' ", outDate);
            if (!string.IsNullOrEmpty(model.line_status))
                sb.AppendFormat(" AND line_status='{0}' ", model.line_status);
            DataTable dt = SqlHelper.ExecuteDataTable(sb.ToString());
            return HCQ2_Common.Data.DataTableHelper.DataTableToIList<T_VROnLineUser>(dt);
        }
    }
}
